package com.aier.admin.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin.entity.IcCard;
import com.aier.common.common.SystemDateFormatUtil;
import com.aier.entrance.common.EntranceICFinalData;

public class IcCardRowMapper implements RowMapper<IcCard> {

    private int type;
    public static final int AND_TIMESTR_SCHOOL = 4;
    public static final int AND_HEADCHOOSE = 3;
    public static final int AND_TIMESTR = 2;
    public static final int AND_ICNAME = 1;
    
    public IcCardRowMapper() {
    }

    public IcCardRowMapper(int type) {
        this.type = type;
    }

    @Override
    public IcCard mapRow(ResultSet rs, int rowNum) throws SQLException {
        IcCard icard = null;
        switch (type) {
            case AND_ICNAME:
                icard = parseIcCard(new IcCard(), rs, "ic");
                icard.setIcName(rs.getString("icName"));
                break;
            case AND_TIMESTR:
                icard = parseIcCard(new IcCard(), rs, "ic");
                icard.setCardnum(icard.getIchead()+icard.getIcbody());
                if(icard.getIcstate()==EntranceICFinalData.IC_STATE_PROXY){
                    icard.setStateName("未分派");
                }else{
                    icard.setStateName("已分派");
                }
                if(icard.getIcpuser()==EntranceICFinalData.IC_PURPOSE_USER_ON){
                    icard.setUsetype("校园门禁");
                }else if(icard.getIcpteacher()==EntranceICFinalData.IC_PURPOSE_TECHER_ON){
                    icard.setUsetype("教师考勤卡");
                }else{
                    icard.setUsetype("无");
                }
                if(icard.getIcstate()==EntranceICFinalData.IC_STATE_USER_ON){
                    icard.setCardstate("已激活");
                }else{
                    icard.setCardstate("未激活");
                }
                
                icard.setTime_str(SystemDateFormatUtil.formatDate(icard.getIcdate(), SystemDateFormatUtil.FORMAT_DATETIME));
                icard.setIcedate_str(SystemDateFormatUtil.formatDate(icard.getIcedate(), SystemDateFormatUtil.FORMAT_DATETIME));
                break;
            case AND_HEADCHOOSE:
                icard = parseIcCard(new IcCard(), rs, "ic");
                break;
            case AND_TIMESTR_SCHOOL:
                icard = parseIcCard(new IcCard(), rs, "ic");
                icard.setCardnum(icard.getIchead()+icard.getIcbody());
                if(icard.getIcstate()==EntranceICFinalData.IC_STATE_SCHOOL){
                    icard.setStateName("未分派");
                }else{
                    icard.setStateName("已分派");
                }
                if(icard.getIcpuser()==EntranceICFinalData.IC_PURPOSE_USER_ON){
                    icard.setUsetype("校园门禁");
                }else if(icard.getIcpteacher()==EntranceICFinalData.IC_PURPOSE_TECHER_ON){
                    icard.setUsetype("教师考勤卡");
                }else{
                    icard.setUsetype("无");
                }
                if(icard.getIcstate()==EntranceICFinalData.IC_STATE_USER_ON){
                    icard.setCardstate("已激活");
                }else{
                    icard.setCardstate("未激活");
                }
                
                icard.setTime_str(SystemDateFormatUtil.formatDate(icard.getIcdate(), SystemDateFormatUtil.FORMAT_DATETIME));
                icard.setIcedate_str(SystemDateFormatUtil.formatDate(icard.getIcedate(), SystemDateFormatUtil.FORMAT_DATETIME));
                break;
            default:
                icard = parseIcCard(new IcCard(), rs);
        }
        return icard;
    }

    public static IcCard parseIcCard(IcCard ic, ResultSet rs) throws SQLException {
        return parseIcCard(ic, rs, null);
    }

    public static IcCard parseIcCard(IcCard ic, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try{
            ic.setIcid(rs.getLong(su+"icid"));
        }catch (SQLException e) {
            ic.setIcid(null);
        }
        try {
            ic.setIchead(rs.getString(su+"ichead"));
        } catch (SQLException e) {
            ic.setIchead(null);
        }
        try {
            ic.setIcbody(rs.getString(su+"icbody"));
        } catch (SQLException e) {
            ic.setIcbody(null);
        }
        try {
            ic.setIcfoot(rs.getString(su+"icfoot"));
        } catch (SQLException e) {
            ic.setIcfoot(null);
        }
        try {
            ic.setIctype(rs.getShort(su+"ictype"));
        } catch (SQLException e) {
            ic.setIctype(null);
        }
        try {
            ic.setIcstate(rs.getShort(su+"icstate"));
        } catch (SQLException e) {
            ic.setIcstate(null);
        }
        
        try{
            ic.setIcpwd(rs.getString(su+"icpwd"));
        }catch (Exception e) {
            ic.setIcpwd(null);
        }
        try{
            ic.setIcarea(rs.getShort(su+"icarea"));
        }catch (Exception e) {
            ic.setIcarea(null);
        }
        try{
            ic.setIcpuser(rs.getShort(su+"icpuser"));
        }catch (Exception e) {
            ic.setIcpuser(null);
        }
        
        try{
            ic.setIcpteacher(rs.getShort(su+"icpteacher"));
        }catch (Exception e) {
            ic.setIcpteacher(null);
        }

        try{
            ic.setIcisn(rs.getString(su+"icisn"));
        }catch (Exception e) {
            ic.setIcisn(null);
        }
        try{
            ic.setIcdate(rs.getTimestamp(su+"icdate"));
        }catch (Exception e) {
            ic.setIcdate(null);
        }
        try{
            ic.setIcedate(rs.getTimestamp(su+"icedate"));
        }catch (Exception e) {
            ic.setIcedate(null);
        }
        return ic;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, IcCard ic) {
        par.put("icid", ic.getIcid());
        par.put("ichead", ic.getIchead());
        par.put("icbody",ic.getIcbody());
        par.put("icfoot", ic.getIcfoot());
        par.put("ictype",ic.getIctype());
        par.put("icstate", ic.getIcstate());
        
        par.put("icpwd",ic.getIcpwd());
        par.put("icarea",ic.getIcarea());
        par.put("icpuser",ic.getIcpuser());
        par.put("icpteacher",ic.getIcpteacher());
        par.put("icisn",ic.getIcisn());
        par.put("icdate",ic.getIcdate());
        par.put("icedate",ic.getIcedate());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, IcCard ic) {
        args.put("icid", ic.getIcid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final IcCard ic) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(ic),
                        new String[] { "icid" });
                parsePS(ps, ic);
                return ps;
            }

        };
    }

    private static String parseSQL(IcCard ic) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into ic_card(");
        int count = 0;
        if (null != ic.getIchead()) {
            sql.append("ichead,");
            count++;
        }

        if (null != ic.getIcbody()) {
            sql.append("icbody,");
            count++;
        }

        if (null != ic.getIcfoot()) {
            sql.append("icfoot,");
            count++;
        }
        if (null != ic.getIctype()) {
            sql.append("ictype,");
            count++;
        }
        if (null != ic.getIcstate()) {
            sql.append("icstate,");
            count++;
        }
        
        
        if (null != ic.getIcpwd()) {
            sql.append("icpwd,");
            count++;
        }
        if (null != ic.getIcarea()) {
            sql.append("icarea,");
            count++;
        }
        if (null != ic.getIcpuser()) {
            sql.append("icpuser,");
            count++;
        }
        if (null != ic.getIcpteacher()) {
            sql.append("icpteacher,");
            count++;
        }
        if (null != ic.getIcisn()) {
            sql.append("icisn,");
            count++;
        }
        if (null != ic.getIcdate()) {
            sql.append("icdate,");
            count++;
        }
        if (null != ic.getIcedate()) {
            sql.append("icedate,");
            count++;
        }        
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, IcCard ic)
        throws SQLException {
        int count = 1;
        if (null != ic.getIchead()) {
            ps.setString(count++,ic.getIchead());
        }
        
        if (null != ic.getIcbody()) {
            ps.setString(count++, ic.getIcbody());
        }


        if (null !=ic.getIcfoot()) {
            ps.setString(count++, ic.getIcfoot());
        }
        if (null != ic.getIctype()) {
            ps.setShort(count++, ic.getIctype());
        }
        if (null != ic.getIcstate()) {
            ps.setShort(count++,ic.getIcstate());
        }
        
        if (null != ic.getIcpwd()) {
            ps.setString(count++,ic.getIcpwd());
        }
        if (null != ic.getIcarea()) {
            ps.setShort(count++,ic.getIcarea());
        }
        if (null != ic.getIcpuser()) {
            ps.setShort(count++,ic.getIcpuser());
        }
        if (null != ic.getIcpteacher()) {
            ps.setShort(count++,ic.getIcpteacher());
        }
        if (null != ic.getIcisn()) {
            ps.setString(count++,ic.getIcisn());
        }
        if (null != ic.getIcdate()) {
            ps.setTimestamp(count++, ic.getIcdate());
        }
        if (null != ic.getIcedate()) {
            ps.setTimestamp(count++, ic.getIcedate());
        }
    }

}
